#include "MyArray.h"

MyArray::MyArray(int length){
        if(length < 0){
                m_length = 0;
        }
        m_length = length;
        m_space = new int[m_length];
}

//赋值(拷贝)构造函数
MyArray::MyArray(const MyArray &obj){
        this->m_length = obj.m_length;
        this->m_space = new int[this->m_length];

        for (int i = 0; i < this->m_length; i++) //数组元素复制 
        {
                this->m_space[i] = obj.m_space[i];
        }
        
}

MyArray::~MyArray(){
        if(m_space){
                delete [] m_space;
                m_length = 0;
        }
}

void MyArray::setData(int index, int value){
        m_space[index] = value;
}

int MyArray::getData(int index){
        return m_space[index];
}

int MyArray::length(){
        return m_length;
}

 //重载[]
int& MyArray::operator[](int i)
{
        return m_space[i];
}

//重载=
MyArray& MyArray::operator=(MyArray &obj)
{
        //先把旧的内存释放掉
        if(m_space){
                delete [] m_space;
                m_length = 0;
        }
        
        //2根据obj的大小 分配内存
        m_length = obj.length();
        m_space = new int[m_length];
        
        //copy数据
        for (int i = 0; i < m_length; i++)
        {
                m_space[i] = obj[i];
        }

        //3 返回自身
        return *this;
}

//重载==
bool MyArray::operator==(MyArray &obj)
{
        if(m_length != obj.length()){
                return false;
        }

        for (int i = 0; i < m_length; i++)
        {
                if(m_space[i] != obj[i]) {
                        return false;
                }
        }
        
        return true;
}

//重载!=
bool MyArray::operator!=(MyArray &obj)
{
        /*
       if(*this == obj){
               return false;
       }
        
        return true;
        */
       return !(*this == obj);
}